﻿//======================================================================
//
//        Copyright (C) 2008 HOHO``
//        All rights reserved
//
//        filename :$safeitemrootname$
//        description :权限管理类.用于权限,角色对应的修改,增加,删除.
//
//        created by 裘中杰 at  $time$
//======================================================================


using System;
using System.Data;
using System.Data.Linq;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Xml.Linq;
using Hisun.Models;

namespace Hisun.Controllers
{
    public class AbcSecurity
    {
        private string lastError;

        public string LastError
        {
            get { return lastError; }
            private set { lastError = value; }
        }
        private HisunDBDataContext db = new HisunDBDataContext();

        public AbcSecurity()
        {

        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="rolename">角色名</param>
        /// <param name="isSystemRole">是否系统角色</param>
        /// <param name="isUserRole">是否用户角色</param>
        /// <returns></returns>
        public int AddRole(string rolename, bool isSystemRole, bool isUserRole)
        {
            TAbc_JS_Role role = new TAbc_JS_Role();
            role.角色名称 = rolename;
            role.系统角色 = isSystemRole;
            role.用户角色 = isUserRole;
            role.权限ID = string.Empty;
            db.TAbc_JS_Roles.InsertOnSubmit(role);
            try
            {
                db.SubmitChanges(ConflictMode.FailOnFirstConflict);
            }
            catch (Exception e)
            {
                lastError = e.Message;
                return -1;
            }
            return 0;
        }

        /// <summary>
        /// 根据角色名称更新角色名
        /// </summary>
        /// <param name="orgRoleName">原始角色名</param>
        /// <param name="newRoleName">新角色名</param>
        /// <returns></returns>
        public int ModifyRoleName(string orgRoleName, string newRoleName)
        {
            if (db.TAbc_JS_Roles.Count(r => r.角色名称 == orgRoleName) == 0)
            {
                lastError = "无此角色名称!";
                return -1;
            }
            try
            {
                var role = (from r in db.TAbc_JS_Roles
                            where r.角色名称 == orgRoleName
                            select r).Single();
                role.角色名称 = newRoleName;
                db.SubmitChanges(ConflictMode.FailOnFirstConflict);
            }
            catch (Exception e)
            {
                lastError = e.Message;
                return -1;
            }
            return 0;
        }

        /// <summary>
        /// 根据角色ID更新角色名
        /// </summary>
        /// <param name="orgRoleName">角色ID</param>
        /// <param name="newRoleName">新角色名</param>
        /// <returns></returns>
        public int ModifyRoleName(int roleID, string newRoleName)
        {
            if (db.TAbc_JS_Roles.Count(r => r.角色ID == roleID) == 0)
            {
                lastError = "无此角色ID!";
                return -1;
            }
            try
            {
                var role = (from r in db.TAbc_JS_Roles
                            where r.角色ID == roleID
                            select r).Single();
                role.角色名称 = newRoleName;
                db.SubmitChanges(ConflictMode.FailOnFirstConflict);
            }
            catch (Exception e)
            {
                lastError = e.Message;
                return -1;
            }
            return 0;
        }


        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleID">角色ID</param>
        /// <returns></returns>
        public int DeleteRole(int roleID)
        {
            if (db.TAbc_JS_Roles.Count(r => r.角色ID == roleID) == 0)
            {
                lastError = "无此角色ID!";
                return -1;
            }
            try
            {
                var role = (from r in db.TAbc_JS_Roles
                            where r.角色ID == roleID
                            select r).Single();
                db.TAbc_JS_Roles.DeleteOnSubmit(role);
            }
            catch (Exception e)
            {
                lastError = e.Message;
                return -1;
            }
            //TODO:这里需要同时删除所有用户中的角色字符
            return 0;
        }


        /// <summary>
        /// 更改权限
        /// </summary>
        /// <param name="authID"></param>
        /// <param name="authname"></param>
        /// <returns></returns>
        public int ModifyAuth(int authID, string authname)
        {
            try
            {
                var auth = (from a in db.TABC_JS_Auths
                            where a.权限ID == authID
                            select a).Single();
                auth.权限名称 = authname;
                db.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (Exception e)
            {
                lastError = e.Message;
                return -1;
            }
            return 0;
        }

        /// <summary>
        /// 增加权限
        /// </summary>
        /// <param name="authname">权限名称</param>
        /// <returns></returns>
        public int AddAuth(string authname)
        {
            try
            {
                TABC_JS_Auth auth = new TABC_JS_Auth();
                auth.权限名称 = authname;
                db.TABC_JS_Auths.InsertOnSubmit(auth);
            }
            catch (Exception e)
            {
                lastError = e.Message;
                return -1;
            }
            return 0;
        }


        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="authID">权限ID</param>
        /// <returns></returns>
        public int DeleteAuth(int authID)
        {
            try
            {
                var auth = (from a in db.TABC_JS_Auths
                            where a.权限ID == authID
                            select a).Single();
                db.TABC_JS_Auths.DeleteOnSubmit(auth);
                db.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (Exception e)
            {
                lastError = e.Message;
                return -1;
            }
            return 0;
        }


        /// <summary>
        /// 设置角色拥有的权限
        /// </summary>
        /// <param name="roleID">角色ID</param>
        /// <param name="authIDs">权限用逗号分隔的字符串</param>
        /// <returns></returns>
        public int SetAuthIDs(int roleID, string authIDs)
        {
            if (db.TAbc_JS_Roles.Count(r => r.角色ID == roleID) == 0)
            {
                lastError = "无此角色ID!";
                return -1;
            }
            try
            {
                var role = (from r in db.TAbc_JS_Roles
                            where r.角色ID == roleID
                            select r).Single();
                role.权限ID = authIDs;
                db.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (Exception e)
            {
                lastError = e.Message;
                return -1;
            }
            return 0;
        }


        /// <summary>
        /// 获得一个角色中所有的权限
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public List<int> GetAuthsOfRole(int roleID)
        {
            List<int> retval = new List<int>();
            try
            {
                var auths = (from a in db.TAbc_JS_Roles
                             where a.角色ID == roleID
                             select a.权限ID).Single();
                if (auths == null)
                    return null;

                var authsStr = auths.Split(',');
                
                foreach (var s in authsStr)
                    retval.Add(Convert.ToInt32(s));
            }
            catch (Exception ex)
            {
                lastError = ex.Message;
                return null;
            }
            return retval;
        }
    }
}
